<?php
global $_RQ,$_AR;
$userid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['status']=$this->order_status;
$return['paytype']=get_pay_types();
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
if($_RQ['id']){
	$_SESSION['hotelid']=$_RQ['id'];
}
$hotelid=$_SESSION['hotelid'];
$return['dai_type']=$this->dai_type;
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'hotelid'=>$hotelid,'userid'=>$userid);
	if(!empty($_RQ['order'])){
		$params['order']=trim($_RQ['order']);
		$return['params']['order']=$_RQ['order'];
	}
	if(!empty($_RQ['status'])){
		if($_RQ['status']!=10){
			$params['status']=intval($_RQ['status']);
		}else{
			$params['status']=5;
			$params['clearstatus']=0;
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
    //$fields=array('id','order','name','intime','hotelid','roomid','ispay','paytype','fee','mid','createtime','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_jd_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$this->model('model')->uporderstatus($v);
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_get('jy_jd_room',array('id'=>$v['roomid']));
		$v['statustitle']=$this->order_status[$v['status']];
		$endtime=strtotime($v['intime'])+$v['day']*24*3600;
		$v['endtime']=date('Y-m-d',$endtime);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
			array('op'=>'dai','title'=>'第三方报单'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('name'=>'num','title'=>'房间数量','type'=>'string'),
			array('type'=>'img_string','title'=>'入住人',
			'data'=>array(
				array('title'=>'入住人：','name'=>'name'),
				array('title'=>'入住电话：','name'=>'mobile')
				)
			),
			array('type'=>'img_string','title'=>'入住时间',
			'data'=>array(
				array('title'=>'入住时间：','name'=>'intime'),
				array('title'=>'离店时间：','name'=>'endtime')
				)
			),
			array('type'=>'img_string','title'=>'费用',
			'data'=>array(
				array('title'=>'订单金额：','name'=>'fee'),
				array('title'=>'包含押金：','name'=>'yafee')
				)
			),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'dai_type','title'=>'预定平台','type'=>'radio','options'=>'dai_type'),			
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),	
		),
		'action'=>array(	
			array('type'=>'post','op'=>'queren','title'=>'确认预定','show'=>array('ispay'=>1,'status'=>0)),
			array('type'=>'modal','op'=>'quxiao','title'=>'同意取消预定','show'=>array('status'=>2)),
			array('type'=>'modal','op'=>'pay','title'=>'确认退款','show'=>array('status'=>3)),
			array('type'=>'post','op'=>'ruzhu','title'=>'确认入住','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'refund','message'=>'确定要帮客户取消预定吗？','title'=>'取消预定','show'=>array('ispay'=>1,'status'=>array(0,1))),
			array('type'=>'post','op'=>'tuifang','title'=>'客户退房','show'=>array('status'=>6)),
			array('type'=>'modal','op'=>'item','title'=>'订单详情'),
		),),
		
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'用户昵称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
		)
	);	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='item'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('id'=>$id));
	if(!$item){
		exi('指定订单不存在~','error');
	}
	$item['hotel']=pdo_get('jy_jd_hotel',array('id'=>$item['hotelid']));
	$item['room']=pdo_get('jy_jd_room',array('id'=>$item['roomid']));
	$item['statustitle']=$this->order_status[$item['status']];
	$endtime=strtotime($item['intime'])+$item['num']*24*3600;
	$item['endtime']=date('Y-m-d',$endtime);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('title'=>'房间数量：','name'=>'num','type'=>'string'),
			array('title'=>'入住人：','name'=>'name','type'=>'string'),
			array('title'=>'入住电话：','name'=>'mobile','type'=>'string'),
			array('title'=>'入住时间：','name'=>'intime','type'=>'string'),
			array('title'=>'离店时间：','name'=>'endtime','type'=>'string'),
			array('title'=>'订单金额：','name'=>'fee','type'=>'string'),
			array('title'=>'订单押金：','name'=>'yafee','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),
		),
	);
	exi($return);
}
if($_RQ['op']=='dai'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$room=pdo_get('jy_jd_room',array('id'=>$post['roomid']));
		if(!$post['intime']){
			exi('请选择入住日期','error');	
		}
		$post['intime']=strtotime($post['intime']);
		$post['intime']=date("Y-m-d",$post['intime']);
		if(!$post['roomid']){
			exi('请选择预定的房间','error');	
		}		
		if(!$post['day']){
			exi('请填写入住时长','error');	
		}
		if(!$post['num']){
			exi('请选择房间数量','error');	
		}
		$innum=$this->model('model')->check_innum($room,$post['intime'],$post['day'],$post['num']);
		if($room['num']-$innum<$post['num']){
			exi('该房型数量不足,请选择其他房型','error');
		}
		if($post['num']==1){
			$allday=$post['intime'];
		}else{
			$allday=';'.$post['intime'].';';
			$time=strtotime($post['intime']);
			for($i=1;$i<$post['day'];$i++){
				$allday.=date("Y-m-d",$time+$i*24*3600).';';
			}
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'hotelid'=>$hotelid,
			'roomid'=>$post['roomid'],
			'name'=>$post['name'],
			'mobile'=>$post['mobile'],
			'day'=>$post['day'],
			'num'=>$post['num'],
			'intime'=>$post['intime'],
			'allday'=>$allday,
			'order'=>get_ordersn('jy_jd_order','order'),
			'fee'=>$post['fee'],
			'dai_type'=>$post['dai_type'],
			'status'=>1,
			'createtime'=>TIMESTAMP		
		);
		if($userid){
			$data['userid']=$userid;
		}
		pdo_insert('jy_jd_order',$data);
		exi('报单成功！','','/manage.order/list/');	
	}
	$return['rooms']=pdo_getall('jy_jd_room',array('hotelid'=>$hotelid));
	$return['item']=array();
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('name'=>'dai_type','title'=>'第三方平台','type'=>'radio','options'=>'dai_type'),
			array('name'=>'roomid','title'=>'预定的房间','type'=>'select','options'=>'rooms'),
			array('title'=>'房间数量：','name'=>'num','type'=>'string'),
			array('title'=>'入住人姓名：','name'=>'name','type'=>'string'),
			array('title'=>'入住电话：','name'=>'mobile','type'=>'string'),
			array('title'=>'入住时间：','name'=>'intime','type'=>'time'),
			array('title'=>'入住天数：','name'=>'day','type'=>'string'),
			array('title'=>'订单金额：','name'=>'fee','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='queren'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));
		
		if($order['status']!=0 || $order['ispay']!=1){
			exi('只有预定状态的订单才能确认','error');	
		}
		pdo_update('jy_jd_order',array('status'=>1),array('id'=>$id));
		exi('确认成功','success','list');			
}
if($_RQ['op']=='quxiao'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));	
		if($order['status']!=2){
			exi('只有申请取消的订单才能确认取消','error');	
		}
		pdo_update('jy_jd_order',array('status'=>3),array('id'=>$id));
		exi('取消成功');	
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','mid','fee','ispay','order','status','createtime'));
	$item['nickname']=pdo_getcolumn('core_members',array('id'=>$item['mid']),'nickname');	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'确认取消订单',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'订单号','name'=>'order','read'=>true),
			array('name'=>'nickname','title'=>'会员昵称','type'=>'string','read'=>true),
			array('name'=>'fee','title'=>'金额','type'=>'string','read'=>true),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status','read'=>true),	
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay','read'=>true),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);		
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->model('refund')->refund_pay($id);
		exi('打款成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$item){
		exi('指定退款订单不存在~','error');
	}
	$member=model('core/core')->member_show($item['mid']);
	$item['wxgid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'wxgid');
	if($member['from_os']==1){
		$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
	}else{
		$item['apptitle']=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
	}
	$item['paytype']=get_pay_types()[$item['paytype']];	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，会显示所属《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('name'=>'apptitle','title'=>'所属','type'=>'copy'),
			array('type'=>'copy','name'=>'paytype','title'=>'退款类型'),
			array('type'=>'copy','name'=>'fee','title'=>'退款金额'),
			array('type'=>'copy','name'=>'fee','title'=>'退款到账金额'),
		),
	);
	exi($return);
}
if($_RQ['op']=='tuifang'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));		
		if($order['status']!=6){
			exi('已入住的订单才能完成退房！','error');	
		}
		pdo_update('jy_jd_order',array('status'=>5),array('id'=>$id));
		exi('退房成功','','list');			
}
if($_RQ['op']=='refund'){
	if(!$_RQ['id']){
		exi('请先选择订单','error');
	}
	$order=pdo_get('jy_jd_order',array('id'=>$_RQ['id']));
	if($order['status']!=0 && $order['status']!=1){
		exi('当前订单不可取消','error');
	}
	$room=pdo_get('jy_jd_room',array('id'=>$order['roomid']));
	if(!$room['is_opentui']){
		exi('该房间不支持退订！','error');
	}
	if($room['tui_day'] || $room['tui_day']){
		$intime=strtotime($order['intime']);
		$tui_daytime=$intime-$room['tui_day']*24*3600;
		$tui_day=date('Y-m-d',$tui_daytime);
		$tui_daytime2=strtotime($tui_day.' '.$room['tui_time']);
		if($tui_daytime2<time()){
			exi('退订时间已过！','error');
		}
	}
	$this->model('notice')->notice_receiveorder($order);
	if($order['ispay']==1){
		$this->model('refund')->check_order_rerefund($order['id']);
		pdo_update('jy_jd_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$this->model('notice')->notice_receiveorder2admin($order);
		exi('已申请取消');
	}
	if($order['ispay']==0){
		pdo_update('jy_jd_order',array('status'=>7),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		exi('已取消');
	}
}
if($_RQ['op']=='ruzhu'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));		
		if($order['status']!=1){
			exi('已预定成功的订单才能入住！','error');	
		}
		pdo_update('jy_jd_order',array('status'=>6),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('入住成功','','list');			
}
if($_RQ['op']=='del'){
	exi('商户暂时不支持删除订单！','error');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jd_order',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！','','list');
}
?>